package com.x.client.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.client.model.SelectTemplate;
import com.x.client.rpc.SelectTemplateRpcService;
import com.x.erp.rpc.CarRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订阅信息
 * @author pansong
 */
@RestController
@RequestMapping(value = "/token/selectTemplate")
public class SelectTemplateController extends BaseController {

	@Reference
	private SelectTemplateRpcService selectTemplateRpcService; // 订阅RpcService

	@Reference
	private CarRpcService carRpcService;//车辆信息服务

	/**
	 * 新增订阅信息
	 * @param selectTemplate
	 * @return
	 */
	@RequestMapping(value = "/insertSelectTemplate", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertSelectTemplate(@Validated SelectTemplate selectTemplate, BindingResult bindingResult) {
		if(bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		// 根据有无订阅Id判断新增还是修改订阅信息
		if(StringUtils.isBlank(selectTemplate.getTemplateId())){
			return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", selectTemplateRpcService.insertSelectTemplate(selectTemplate));
		} else {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功", selectTemplateRpcService.updateSelectTemplate(selectTemplate));
		}
	}

	/**
	 * 删除订阅信息
	 * @param selectTemplate
	 * @return
	 */
	@RequestMapping(value = "/deleteSelectTemplate", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel deleteSelectTemplate(@Validated SelectTemplate selectTemplate, BindingResult bindingResult) {
		if(bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功", selectTemplateRpcService.deleteSelectTemplate(selectTemplate));
	}

	/**
	 * 查询订阅信息
	 * @param selectTemplate
	 * @return
	 */
	@RequestMapping(value = "/selectSelectTemplate", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectSelectTemplate(@Validated SelectTemplate selectTemplate, BindingResult bindingResult) {
		if(bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		selectTemplate = selectTemplateRpcService.selectSelectTemplate(selectTemplate);
		// 如果没有订阅信息，返回新建对象
		if(selectTemplate == null){
			selectTemplate = new SelectTemplate ();
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", selectTemplate);
	}

	/**
	 * 查询订阅列表信息
	 * @param selectTemplate
	 * @return
	 */
	@RequestMapping(value = "/selectSelectTemplateList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectSelectTemplateList(@Validated SelectTemplate selectTemplate, BindingResult bindingResult) {
		if(bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		List<SelectTemplate> res = selectTemplateRpcService.selectSelectTemplateList(selectTemplate);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}
}
